/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhelper01;

import java.awt.LayoutManager;
import java.awt.Panel;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import jhelper.blls.AddressBook;



/**
 *
 * @author Administrator
 */
public class AddressBookPanel extends JPanel{
    Panel p,pleft,pleft_main,pleft_down;
    JPanel pQuery;
    JScrollPane sp;
    JList lall;
    DefaultListModel listmode;
    JButton btnQuery,btnAdd,btnSave,btnDel;
    JTextField txtName,txtOffice,txtHome,txtMobile,txtQQ,txtEmail,txtOther,txtCondition;
    JLabel lName,lOffice,lHome,lMobile,lQQ,lEmail,lOther,lMsg;
    Hashtable h;
    public AddressBookPanel(){
        p=new Panel();
        pleft=new Panel();
        pleft_main=new Panel();
        pleft_down=new Panel();
        pQuery=new JPanel();
        btnQuery=new JButton("查询");
        btnSave=new JButton("保存");
        btnDel=new JButton("删除");
        btnAdd=new JButton("添加");
        txtName=new JTextField("",20);
        txtName.setEnabled(false);
        txtOffice=new JTextField("",20);
        txtHome=new JTextField("",20);
        txtMobile=new JTextField("",20);
        txtQQ=new JTextField("",20);
        txtEmail=new JTextField("",20);
        txtOther=new JTextField("",20);
        txtCondition=new JTextField("",10);
        lName=new JLabel("姓名",JLabel.CENTER);
        lOffice=new JLabel("办公电话",JLabel.CENTER);
        lHome=new JLabel("家庭电话",JLabel.CENTER);
        lMobile=new JLabel("手机",JLabel.CENTER);
        lQQ=new JLabel("QQ",JLabel.CENTER);
        lEmail=new JLabel("Email",JLabel.CENTER);
        lOther=new JLabel("其他",JLabel.CENTER);
        lMsg=new JLabel("请输入要查询的联系人姓名:");
        sp=new JScrollPane();
        h=new AddressBook().getAllCards();
        listmode=new DefaultListModel();
        if(h! =null){
        listmode.addElement("点击如下名单可查看相关信息");
        Enumeration e=h.keys();
        while(e.hasMoreElements()){
            listmode.addElement(e.nextElement());
        }
    }else{
    listmode.addElement("通讯录为空");
    txtName.setEnabled(true);
}
    lall=new JList(listmode);
    sp.setViewportView(lall);
    pQuery.setBorder(BorderFactory.createTitledBorder("联系人查询"));
    pQuery.setLayout(new FlowLayout(FlowLayout.CENTER));
    pQuery.add(lMsg);
    pQuery.add(txtCondition);
    pQuery.add(btnQuery);
    pleft_main.setLayout(new GridLayout(7,2));
    pleft_main.add(txtName);
    pleft_main.add(lMobile);
    pleft_main.add(txtMobile);
    pleft_main.add(lOffice);
    pleft_main.add(txtOffice);
    pleft_main.add(lHome);
    pleft_main.add(txtHome);
    pleft_main.add(lQQ);
    pleft_main.add(txtQQ);
    pleft_main.add(lEmail);
    pleft_main.add(txtEmail);
    pleft_main.add(lOther);
    pleft_main.add(txtOther);
    p.setLayout(new FlowLayout(FlowFayout.CENTER));
    p.add(pleft_main);
    pleft_down.setLayout(new FlowLayout(Flowlayout.CENTER));
    pleft_down.add(pQuery);
    pleft_down.add(btnAdd);
    pleft_down.add(btnSave);
    pleft_down.add(btnDel);
    p.setLayout(new BorderLayout());
    pleft.add("Center",p);
    pleft.add("South",pleft_down);
    this.setLayout(new BorderLayout());
    this.add("East",sp);
    this.add("Center",pleft);
    
    if(h! ==null){
       lall.setSelectedIndex(l);
       showCard(lall.getSelectedValue().toString());
}
    lall.addListSelectionListener(new ListSelectionListener()){
        public void valueChanged(ListCelectionEvent e){
            int index=lall.getSelectedIndex();
            Object name=lall.getSelectedValue();
            if(index>0){
                showCard(name.toString());
                
            }
        }
    });

      btnAdd.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
          clearText();

}
});
     btnSave.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
          if(txtname.isEnabled()==true){
           addnew();
             }else{
             update();
}
refreshList();

});
 btnDel.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
delete();
refreshList();
}
});
 btnQuery.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
         String name=txtCondition.getText();
showCard(name);
}
});
   
}
    
public void delete(){
if(txtName.getText().trim().length()==0){
JQptionPane.showMessageDialog(null,"必须输入联系人姓名")；
return;
}
String name=txtName.getText();
txtName.setEnabled(false);
if(new AddressBook().delCard(name==true){
JQptionPane.showMessageDialog(null,"成功删除了"+name+"的联系信息")；
}else{
JQptionPane.showMessageDialog(null,"联系人"+name+"的信息删除失败")；
}
}

public void refreshList(){
h=new addressbook().getAllCards();
if(h!==null){
listmode.clear():
listmode.addElement("点击如下名单可查看相关信息");
Enumeration e=h.key();
while(e.hasMoreElements()){
listmode.addElement(e.nextElement());

}
}else{
listmode.clear();
listmode.addElement("通讯录为空");
}
}
public void showCard(String name){
Object temp=h.get(nmae);
if(temp==null){
JQptionPane.showMessageDialog(null,"没有找到联系人:"+name,"提示",JOptionPane.ERROR_MESSAGE);
return;
}
AddressBookBean b==(AddressBookBean)temp;
txtName.setText(b.getName());
txtName.setEditable(false));
txtMobile.setText(b.getMobile());
txtOffice.setText(b.getOffice()));
txtHome.setText(b.getHome());
txtQQ.setText(b.getQQ());
txtEmail.setText(b.getEmail());
txtother.setText(b.getOther());
}
   public void clearText(){
txtName.setText("");
txtName.setEnabled(true);
txtName.setEnabledt(true);
txtMobile.setText("");
txtOffice.setText("");
txtHome.setText("");
txtQQ.setText("");
txtEmail.setText("");
txtOther.setText("");
}
 public void addNew(){
if(txtName.getText().trim().length()==0){
JQptionPane.showMessageDialog(null,"必须输入联系人姓名");
return;
}
String name=txtName.getText();
String mobile=txtMobile.getText();
mobile=mobile.trim().length()==0?"空";mobile.trim();
String office=txtOffice.getOffile.Text();
office=office.trim().length()==0?"空";office.trim();
String home=txtHome.getText();
home=home.trim().length()==0?"空";home.trim();
String qq=txtQQ.getText();
qq=qq.trim().length()==0?"空";qq.trim();
String email=txtEmail.getText();
email=email.trim().length()==0?"空";email.trim();
String other=txtother.getText();
other=other.trim().length()==0?"空";other.trim();
AddressBookBean newcard=new AddressBookBean(name,mobile,office,home,qq,email,other);
if(new AddressBooks().newCard(newcard)==true){
JOptionPane.showMessageDialog(null,"成功添加了"+name+"的联系信息");

}else{
JOptionPane.showMessageDialog(null,"联系人"+name+"的信息添加失败");
}
txtName.setElement(false);
}      
    public void update(){
if(txtName.getText().trim().length()==0){
JOptionPane.showMessageDialog(null,"必须输入联系人姓名");
return;
}
String name=txtName.getText();
String mobile=txtMobile.getText();
mobile=mobile.trim().length()==0?"空":mobile.trim();
String office=txtOffice.Text();
office=office.trim().length()==0?"空":office.trim();
String home=txtHome.getText();
home=home.trim().length()==0?"空":home.trim();
String qq=txtQQ.getText();
qq=qq.trim().length()==0?"空":qq.trim();
String email=txtEmail.getText();
email=email.trim().length()==0?"空":email.trim();
String other=txtother.getText();
other=other.trim().length()==0?"空":other.trim();
AddressBookBean oldcard=new AddressBookBean(name,mobile,office,home,qq,email,other);
if(new AddressBooks().modifyCard(oldcard)==true){
   JOptionPane.showMessageDialog(null,"成功修改了"+name+"的联系信息");

}else{
      JOptionPane.showMessageDialog(null,"联系人"+name+"的信息修改失败");
     }
  }
public void paintComponent(Graphics g){
  super.paintComponent(g);

}
public static void main(String[] args) {
      Frame f=new Frame();
      f.add(new AddressbookPanel());
      f.pack();
      f.show();
      f.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
            System.exit(0);
         }
     });

  }
}    
   

